package com.woniuxy.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.user.entity.UserInfo;
import com.woniuxy.user.entity.UserLog;
import com.woniuxy.user.mapper.UserInfoMapper;
import com.woniuxy.user.mapper.UserLogMapper;

import com.woniuxy.user.repository.UserLogRepository;
import com.woniuxy.user.service.IUserLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.user.utils.JwtUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * <p>
 * 用户操作日志表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-05-13
 */
@Service
public class UserLogServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implements IUserLogService {

    @Resource
    private UserLogRepository userLogRepository;
    @Resource
    private UserInfoMapper userInfoMapper;

    @Override
    public void logUserAction(Integer userId, String userName, String actionType, String actionDesc, Integer successFlag, String errorMsg) {

        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        UserLog userLog = new UserLog();
        userLog.setUserId(userId);
        userLog.setUserName(userName);
        userLog.setActionType(actionType);
        userLog.setActionDesc(actionDesc);
        userLog.setIpAddress(request.getRemoteAddr());
        userLog.setUserAgent(request.getHeader("user-agent"));
        userLog.setRequestUrl(request.getRequestURL().toString());
        userLog.setRequestMethod(request.getMethod());
        userLog.setSuccessFlag(successFlag);
        userLog.setErrorMsg(errorMsg);
        userLog.setCreateTime(LocalDateTime.now());
        userLogRepository.insert(userLog);
    }
    @Override
    public void logDeleteAddressAction(String userId, String actionDesc, Integer successFlag, String errorMsg) {
        try {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();

            // 根据 userId 查询用户信息
            QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", userId);
            UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
            String userName = userInfo != null ? userInfo.getLoginName() : "Unknown";

            // 创建日志记录
            UserLog userLog = new UserLog();
            userLog.setUserId( Integer.parseInt(userId));
            userLog.setUserName(userName);
            userLog.setActionType("DELETE_ADDRESS");
            userLog.setActionDesc(actionDesc);
            userLog.setIpAddress(request.getRemoteAddr());
            userLog.setUserAgent(request.getHeader("user-agent"));
            userLog.setRequestUrl(request.getRequestURL().toString());
            userLog.setRequestMethod(request.getMethod());
            userLog.setSuccessFlag(successFlag);
            userLog.setErrorMsg(errorMsg);
            userLog.setCreateTime(LocalDateTime.now());

            // 插入日志记录
            userLogRepository.insert(userLog);
        } catch (Exception e) {
            e.printStackTrace();
            // 如果日志记录失败，可以记录到本地日志文件中
            log.error("日志记录失败", e);
        }
    }

}
